/**
 * Created by MD-3 on 2017/8/3.
 */


function addOverlay() {
    var preDev, childDev;
    var childDevlist;
    var fitViewMarkerList = [];
    var clustermarkers = [];
    var marker;
    for(var i in deviceMarkerList){
        preDev = deviceMarkerList[i];
        childDevlist = preDev.childDevlist;

        marker = addMarker(preDev);
        preDev.marker = marker;
        clustermarkers.push(marker);
        for(var j in childDevlist){
            childDev = childDevlist[j];
            marker = addMarker(childDev);
            childDev.marker = marker;
            childDev.circle = addCircle(childDev);
            childDev.polyline = addPolyline(preDev,childDev);
            clustermarkers.push(marker);

            //报警提醒
            if(1==childDev.alarmState){
                fitViewMarkerList.push(marqueeAlarm(childDev));
            }
        }
    }

    if(fitViewMarkerList && 0!=fitViewMarkerList.length) {
        var newCenter = map.setFitView(fitViewMarkerList);
        fitViewMarkerList = [];
    }

    addCluster(2, clustermarkers);
}

function addMarker(device) {
    var deviceMarker = device.deviceMarker;
    var icon = getMarkerIcon(device);
    if (deviceMarker) {
        deviceMarker.setIcon(icon);
    } else {
        deviceMarker = addDevMarker(device,icon);
        addMarkerListener(deviceMarker, device.id);
    }
    return deviceMarker;

}

function getMarkerIcon(device) {
    var type        = device.type;
    var workState   = device.workState;
    var alarmState  = device.alarmState;
    var image;
    var width, height;
    width = height = 24;
    if(type && 0 == type){
        image = resource_mock.IMG_MARKER_TYPE0;
        width = height = 36;
    } else if(alarmState && 1 == alarmState){
        image = resource_mock.IMG_MARKER_ALARM;
    } else if(workState && 1 == workState) {
        image = resource_mock.IMG_MARKER_OFFLINE;
    } else if(type && 1 == type){
        image = resource_mock.IMG_MARKER_TYPE1;
    }

    var icon = new AMap.Icon({
        size: new AMap.Size(width, height),                   //图标大小
        image: image,
        //imageOffset: new AMap.Pixel(0, 0),                  //图标取图偏移量,可配合size使用
        imageSize: new AMap.Size(width, height)              //图标所用图片大小,会造成无法显示图片？
    });
    return icon;
}

function addDevMarker(device,icon) {
    var type        = device.type;
    var name        = device.name;
    var lng         = device.lng;
    var lat         = device.lat;

    var offsetx, offsety;
    var width  = icon.getImageSize().getWidth( );
    var height = icon.getImageSize().getHeight( );
    if(type && 0 == type){
        offsetx = 0;        offsety = -height;
    } else {
        offsetx = -width/2; offsety = -height/2;
    }
    var marker = new AMap.Marker({
        map: map,
        position: [lng, lat],
        icon: icon,                                               //icon是否对网速有影响 ？？
        offset: new AMap.Pixel(offsetx, offsety),
        //title: name,                  //鼠标滑过点标记时的文字提示，不设置则鼠标滑过点标无文字提示
        label: {content: name, offset: new AMap.Pixel(width, height/2)},
        topWhenClick: true
    });
    return marker;
}

function addCircle(device) {
    var lng         = device.lng;
    var lat         = device.lat;
    var radius      = device.radius;
    var workState   = device.workState;
    var alarmState  = device.alarmState;

    var lineColor = getStrokeColor(workState,alarmState);
    var circle = device.circle;
    if(circle){
        circle.setOptions({
            radius: radius,                          //半径
            strokeColor: lineColor,                 //线颜色
            fillColor: lineColor
        });
    } else {
        circle = addDevCircle(lng, lat, radius, lineColor);
    }
    return circle;
}

function addDevCircle(lng, lat, radius, lineColor) {
    if (!radius) return;
    var circle = new AMap.Circle({
        map: map,
        center: [lng, lat],                      // 圆心位置
        radius: radius,                          //半径
        strokeColor: lineColor,                 //线颜色
        strokeOpacity: 1,                       //线透明度
        strokeWeight: 1,                        //线粗细度
        strokeStyle: "dashed",                 //轮廓线样式，实线:solid，虚线:dashed
        fillColor: lineColor,                   //填充颜色
        fillOpacity: 0.1                        //填充透明度
    });
    return circle;
}

function addPolyline(preDev,childDev) {
    var workState   = childDev.workState;
    var alarmState  = childDev.alarmState;
    var polyline    = childDev.polyline;
    var preLngLat   = new AMap.LngLat(preDev.lng, preDev.lat);
    var childLngLat = new AMap.LngLat(childDev.lng, childDev.lat);

    var lineColor = getStrokeColor(workState, alarmState);
    if(polyline) {
        polyline.setOptions({
            strokeColor: lineColor,
        });
    } else {
        polyline = addDevPolyline([preLngLat,childLngLat],lineColor);
    }
    return polyline;
}

function addDevPolyline(lineArr, lineColor) {
    var polyline = new AMap.Polyline({
        map: map,
        path: lineArr,          //设置线覆盖物路径
        strokeColor: lineColor, //线颜色
        strokeOpacity: 1,       //线透明度
        strokeWeight: 2,        //线宽，单位：像素
        strokeStyle: "dashed",   //线样式,实线:solid，虚线:dashed
        strokeDasharray: [10, 5] //补充线样式
    });
    return polyline;
}



function getStrokeColor(workState, alarmState) {
    var strokeColorList = [
        strokeColor_mock.COLOR0, strokeColor_mock.COLOR1,
        strokeColor_mock.COLOR2, strokeColor_mock.COLOR3,
        strokeColor_mock.COLOR4, strokeColor_mock.COLOR5,
        strokeColor_mock.COLOR6, strokeColor_mock.COLOR7,
        strokeColor_mock.COLOR8, strokeColor_mock.COLOR9 ];
    var index = 3;
    if (alarmState && alarmState == 1) {
        index = 2;
    } else if (workState && workState == 1) {
        index = 1;
    }else if (workState && workState == 0) {
        index = 0;
    }
    return strokeColorList[index];
}


function addCluster(tag, markers) {
    var cluster;
    if (tag == 2) {                             //完全自定义
        var count  = markers.length;
        var _renderCluserMarker = function (context) {
            var factor = Math.pow(context.count/count,1/18);
            var div = document.createElement('div');
            var Hue = 180 - factor* 180;
            var bgColor = 'hsla('+Hue+',100%,50%,0.7)';
            var fontColor = 'hsla('+Hue+',100%,20%,1)';
            var borderColor = 'hsla('+Hue+',100%,40%,1)';
            var shadowColor = 'hsla('+Hue+',100%,50%,1)';
            div.style.backgroundColor = bgColor
            var size = Math.round(30 + Math.pow(context.count/count,1/5) * 20);
            div.style.width = div.style.height = size+'px';
            div.style.border = 'solid 1px '+ borderColor;
            div.style.borderRadius = size/2 + 'px';
            div.style.boxShadow = '0 0 1px '+ shadowColor;
            div.innerHTML = context.count;
            div.style.lineHeight = size+'px';
            div.style.color = fontColor;
            div.style.fontSize = '14px';
            div.style.textAlign = 'center';
            context.marker.setOffset(new AMap.Pixel(-size/2,-size/2));
            context.marker.setContent(div)
        };
        cluster = new AMap.MarkerClusterer(map,markers,{
            gridSize: 80,
            maxZoom: 13,
            renderCluserMarker:_renderCluserMarker
        });
    } else if (tag == 1) {                      //自定义图标
        var sts = [{
            url: "http://a.amap.com/jsapi_demos/static/images/blue.png",
            size: new AMap.Size(32, 32),
            offset: new AMap.Pixel(-16, -16)
        }, {
            url: "http://a.amap.com/jsapi_demos/static/images/green.png",
            size: new AMap.Size(32, 32),
            offset: new AMap.Pixel(-16, -16)
        }, {
            url: "http://a.amap.com/jsapi_demos/static/images/orange.png",
            size: new AMap.Size(36, 36),
            offset: new AMap.Pixel(-18, -18)
        },{
            url: "http://a.amap.com/jsapi_demos/static/images/red.png",
            size: new AMap.Size(48, 48),
            offset: new AMap.Pixel(-24, -24)
        },{
            url: "http://a.amap.com/jsapi_demos/static/images/darkRed.png",
            size: new AMap.Size(48, 48),
            offset: new AMap.Pixel(-24, -24)
        }];
        cluster = new AMap.MarkerClusterer(map, markers, {
            styles: sts,
            gridSize:80
        });
    } else {                                 //默认样式
        cluster = new AMap.MarkerClusterer(map, markers,{gridSize:80});
    }
}



function addMarkerListener(deviceMarker, id) {
    deviceMarker.on('click', function () {
        openDeviceInfo(id);                 //信息窗口
    });
}

function marqueeAlarm(childDev) {
    if(!childDev.alarmState && 1!=childDev.alarmState) {
        return;
    }
    var id = childDev.id;
    var alarmlist = childDev.alarmlist;
    if(alarmlist && 0<alarmlist.length) {                       //1==alarmState 可能需要修改
        bjAlarmAmount = bjAlarmAmount == null ? 0 : bjAlarmAmount;
        //play_bjAlarm();                                       //报警音乐提醒

        $.each(alarmlist, function(index,alarm){
            var alarmId = alarm.alarmId;
            var alarmTime = alarm.alarmTime;
            var ulId = 'marquee-content-items';
            var liId = alarmId;                               //需要alarmId 和 alarmTime
            var text = '<a style="color:#fff" onclick="mapPanTo(this)" deviceid=' + id + '>发生报警：' + name +' ' + liId + ' ' + alarmTime + '</a>';
            addliById(ulId, liId, text);                      //报警滚动消息
        });
    }
    return childDev.marker;
}



